package com.ethereal.petmanagement.dao.impl;

import com.ethereal.petmanagement.dao.IPetDao;
import com.ethereal.petmanagement.dto.PetDTO;
import com.ethereal.petmanagement.dto.ResponseDTO;
import com.ethereal.petmanagement.util.DatabaseUtil;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class PetDaoImpl implements IPetDao {

    //添加宠物
    @Override
    public ResponseDTO<Integer> addPet(PetDTO petDTO) {
        System.out.println("ddd");
        String sql = "insert into pet (id, user_id, name, species, birthday, gender, avatar_url, is_public, create_time, pet_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        int rows;
        try (
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql语句
        ) {
            //添加参数
            pstmt.setObject(1, petDTO.getId());
            pstmt.setObject(2, petDTO.getUserId());
            pstmt.setString(3, petDTO.getName());
            pstmt.setString(4, petDTO.getSpecies());
            pstmt.setTimestamp(5, petDTO.getBirthday());
            pstmt.setInt(6, petDTO.getGender());
            pstmt.setString(7, petDTO.getAvatarUrl());
            pstmt.setInt(8, petDTO.getIsPublic());
            pstmt.setTimestamp(9, petDTO.getCreateTime());
            pstmt.setInt(10, petDTO.getPetType());
            //操作数据库，将返回值给rows
            rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
            return ResponseDTO.success(rows);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<PetDTO> getPetList(PetDTO petDTO) {
        String sql = "select * from pet where user_id = ?";
        try (
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql语句
        ) {
            //添加参数
            pstmt.setObject(1, petDTO.getUserId());
            //操作数据库，将返回值给rows
            ResultSet rs = pstmt.executeQuery(); //查找结果
            List<PetDTO> petDTOList = new ArrayList<>();
            while (rs.next()) {  // 使用while循环而不是if
                PetDTO petDTO1 = new PetDTO(); // 每次循环创建新的DTO对象
                petDTO1.setId((BigInteger) rs.getObject("id"));
                petDTO1.setName(rs.getString("name"));
                petDTO1.setSpecies(rs.getString("species"));
                petDTO1.setBirthday(rs.getTimestamp("birthday"));
                petDTO1.setGender(rs.getInt("gender"));
                petDTO1.setCreateTime(rs.getTimestamp("create_time"));
                petDTO1.setIsPublic(rs.getInt("is_public"));
                petDTO1.setAvatarUrl(rs.getString("avatar_url"));
                petDTOList.add(petDTO1); // 将DTO对象添加到列表
            }
            return petDTOList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseDTO<Integer> deletePet(PetDTO petDTO) {
        String sql = "delete from pet where id = ?";
        try (
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql语句
        ) {
            //添加参数
            pstmt.setObject(1, petDTO.getId());
            //操作数据库，将返回值给rows
            int rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
            return ResponseDTO.success(rows);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //修改宠物
    @Override
    public ResponseDTO<Integer> updatePet(PetDTO petDTO) {
        String sql = "update pet set name = ?, species = ?, birthday = ?, gender = ?, is_public = ? where id = ?";
        int rows;
        try (
                Connection conn = DatabaseUtil.getConnection(); //连接数据库
                PreparedStatement pstmt = conn.prepareStatement(sql); //配置sql语句
        ) {
            //添加参数
            pstmt.setString(1, petDTO.getName());
            pstmt.setString(2, petDTO.getSpecies());
            pstmt.setTimestamp(3, petDTO.getBirthday());
            pstmt.setInt(4, petDTO.getGender());
            pstmt.setInt(5, petDTO.getIsPublic());
            pstmt.setObject(6, petDTO.getId());
            //操作数据库，将返回值给rows
            rows = pstmt.executeUpdate();
            conn.commit(); //永久更新
            return ResponseDTO.success(rows);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
